<?php

namespace RedMVC;

/**
 * Registry class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Registry{

    const NAMESPACE_DEFAULT  = 'RedMVC';

    protected static $_storage  = array();    
    protected static $_instance = null;
    protected static $_currentNamespace;
    
    private function __construct(){
    
    }

    public static function getInstance($namespace = self::NAMESPACE_DEFAULT){
        if(null === self::$_instance){
            self::$_instance = new self();
        }
        self::$_currentNamespace = $namespace;
        return self::$_instance;
    }
    
    public static function getStorage(){
        return self::$_storage;
    }
    
    public static function set($key, $value, $namespace = self::NAMESPACE_DEFAULT){
        self::$_storage[$namespace][$key] = $value;
    }
    
    public static function get($key, $namespace = self::NAMESPACE_DEFAULT){
        if(null === $key && isset(self::$_storage[$namespace])){
            return self::$_storage[$namespace];
        }
        if(isset(self::$_storage[$namespace][$key])){
            return self::$_storage[$namespace][$key];
        }
        return null;
    }
    
    public function __set($key, $value){
        self::$_storage[self::$_currentNamespace][$key] = $value;
    }
    
    public function __get($key){
        if(isset(self::$_storage[self::$_currentNamespace][$key])){
            return self::$_storage[self::$_currentNamespace][$key];
        }
        return null;
    }
    
    public function __isset($key){
        return isset(self::$_storage[self::$_currentNamespace][$key]);
    }
    
    public function __unset($key){
        unset(self::$_storage[self::$_currentNamespace][$key]);
    }
    
    public function namespaceUnset(){
        unset(self::$_storage[self::$_currentNamespace]);
    }
    
    public function namespaceExists(){
        return isset(self::$_storage[self::$_currentNamespace]);
    }

}